package com.mmall.concurrency.thread.one;

import java.util.Queue;

/**
 * 功能描述：
 *
 * @author 吴明泽
 * @version 1.0
 * @date 2022-10-20 19:49:48
 */
public class Producer implements Runnable {
    private Queue<String> bags;

    private int maxSize;

    public Producer(Queue<String> bags, int maxSize) {
        this.bags = bags;
        this.maxSize = maxSize;
    }


    @Override
    public void run() {
        int i = 0;
        while (true) {
            i++;
            synchronized (bags) {
                if (bags.size() == maxSize) {
                    try {
                        System.out.println("生产者满了");
                        bags.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }

                System.out.println("生产者生产：bag"+i);
                bags.add("bag" + i);
                bags.notify(); // 提示消费者可以消费

            }
        }
    }
}
